<template>
	<view>
		<!-- 地图显示 -->
		<orderMap :address="orderForm.address"></orderMap>

		<!-- 订单详情 -->
		<view class="order-views">
			<view class="grid-box">
				<view class="u-flex">
					<view class="u-m-r-20"><u-icon name="clock" size="80" :color="$u.color.infoDark"></u-icon></view>
					<view class="u-flex-1">
						<view class="head-title u-m-b-10"><text>{{orderForm.managestate}}</text></view>
						<view class="tips-txt">请尽快联系客户处理</view>
					</view>
				</view>
			</view>
			
			<view class="grid-box">
				<view class="u-flex">
					<view class="u-flex-1" @click="goMap(orderForm.address)">
						<text class="u-font-36 u-font-bold">[{{orderForm.address.cityname}}]</text>
						<text class="u-font-36">{{orderForm.address.address}}</text>
					</view>
					<view>
						<text class="iconfont icon-youjiantou u-tips-color"></text>
					</view>
				</view>
				<view class="u-m-t-20">
					<text class="u-tips-color">{{orderForm.address.name}}</text>
					<text class="u-tips-color u-p-l-10">{{hidePhoneNumber(orderForm.address.mobile)}}</text>
				</view>
				<!-- <view class="u-p-t-20">
					<u-button type="primary" @click="call(orderForm.address.mobile)"><u-icon name="phone-fill" size="36"></u-icon>电话联系</u-button>
				</view> -->
			</view>
			
			<view class="grid-box">
				<view class="head-title u-m-b-20"><text>订单信息</text></view>
				<view class="msg-box">
					<view class="item">
						<text class="label">订单编号：</text>
						<view class="u-flex u-flex-1">
							<view class="u-flex-1 text">
								<u-tag type="info" size="mini" text="首" v-if="orderForm.firstorder"/>
								<text>{{orderForm.number}}</text>
							</view>
							<view>
								<text class="u-tips-color u-p-l-20 copy" @click="copy(orderForm.number)">复制</text>
							</view>
						</view>
					</view>
					<view class="item">
						<text class="label">下单时间：</text>
						<view class="text">{{$u.timeFormat(orderForm.createddatetime, 'yyyy-mm-dd hh:MM:ss')}}</view>
					</view>
					<view class="item">
						<text class="label">需求回收品：</text>
						<view class="text">
							<u-tag 
								class="u-m-l-5"
								v-for="(item, index) in orderForm.estimated" 
								:key="index"
								:text="item.names"
								type="info" 
								size="mini"
							/>
						</view>
					</view>
					<view class="item" @click="seePhoto(0)">
						<text class="label">用户拍照：</text>
						<view class="text">
							<view class="u-flex" style="justify-content: flex-end;">
								<view class="u-m-l-10" v-for="(item,index) in orderForm.pics" :key="index">
									<u-image width="60rpx" height="60rpx" :src="utils.getPic(item)" shape="circle" @click="seePhoto(index)"></u-image>
								</view>
							</view>
						</view>
					</view>
					<!-- <view class="item">
						<text class="label">上门时间：</text>
						<view class="text" v-if="orderForm.doortime">
							{{$u.timeFormat(orderForm.doortime, 'yyyy-mm-dd hh:MM:ss')}}</view>
						<view class="text" v-else>尽快上门</view>
					</view> -->
				</view>
			</view>
		
			<view class="grid-box" v-if="process.length > 0">
				<view class="head-title u-m-b-30"><text>订单进程</text></view>
				<u-time-line>
					<u-time-line-item v-for="(item,index) in process" :key="index">
						<!-- 此处没有自定义左边的内容，会默认显示一个点 -->
						<template v-slot:content>
							<view>
								<view class="u-order-title u-m-b-10">{{item.state}}</view>
								<view class="u-order-desc" v-if="item.remarks">
									<text v-if="item.state == '已取消'">取消原因：</text>
									<text>{{item.remarks}}</text>
								</view>
								<view class="u-order-time"><u-icon
										name="calendar-fill"></u-icon>{{$u.timeFormat(item.createddatetime, 'yyyy-mm-dd hh:MM:ss')}}
								</view>
							</view>
						</template>
					</u-time-line-item>
				</u-time-line>
			</view>
		</view>
		<view class="navigation">
			<view class="right">
				<!-- <u-button class="navbtn">联系客服</u-button> -->
				<!-- <u-button class="navbtn" @click="showCancel = true">拒单</u-button> -->
				<u-button type="primary" class="navbtn" @click="receiving(orderId)">接单</u-button>
			</view>
		</view>
		<!-- 拒绝订单 -->
		<u-popup v-model="showCancel" mode="center" border-radius="14" z-index="999" closeable>
			<view class="u-p-40" style="width: 600rpx;">
				<view class="u-p-t-30 u-p-b-15">请填写拒绝订单原因：</view>
				<view class="u-p-b-30">
					<u-input v-model="cancelForm.reason" type="textarea" :border="true" />
				</view>
				<u-button type="error" class="navbtn" @click="refuse">确定</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import orderMap from '../orderMap/orderMap.vue';

	import uniCopy from '@/common/xb-copy/uni-copy.js'

	export default {
		components:{
			orderMap
		},
		props: {
			orderId: 0,
			orderForm: {}
		},
		data() {
			return {
				process: [],
				// 拒单
				showCancel: false,
				cancelForm: {
					id: 0,
					reason: ""
				},
			}
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				header: state => state.Authorization
			})
		},
		created() {
			console.log('orderId',this.orderId)
			this.getProcess()
		},
		methods: {
			hidePhoneNumber(phoneNumber) {
			    if (!phoneNumber || phoneNumber.length !== 11) {
			      return phoneNumber; // 如果电话号码为空或长度不是11位，直接返回
			    }
			    return phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(7);
			},
			// 复制方法
			copy(font) {
				uniCopy({
					content: font,
					success: (res) => {
						uni.showToast({
							title: res,
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: 'none',
							duration: 3000,
						})
					}
				})
			},
			// 接单
			receiving(id) {
				uni.showModal({
					title: '提示',
					content: '您确定要接受订单吗?',
					success: (res) => {
						if (res.confirm) {
							// this.swiperCurrent = 1
							this.$u.get('/User/Order/Receiving?id=' + id).then((res) => {
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
								this.swiperCurrent = 1
								this.$emit('update')
							})
						}
					}
				})
			},
			// 拒单
			refuse() {
				uni.showModal({
					title: '提示',
					content: '您确定要拒绝订单吗?',
					success: (res) => {
						if (res.confirm) {
							this.cancelForm.id = this.orderId
							this.$u.post('/User/Order/Refuse', this.cancelForm).then((res) => {
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
								// this.getOrderList(0)
							})
						}
					}
				})
			},
			// 获取订单流程
			getProcess() {
				this.$u.get('/User/Order/Process?id=' + this.orderId).then((res) => {
					this.process = res
				})
			},
			seePhoto(current) {
				let url = []
				this.orderForm.pics.forEach((item)=>{
					url.push(this.utils.getPic(item))
				})
				uni.previewImage({
					current: current,
					urls: url
				});
			},
			// 查看地图
			goMap(data) {
				uni.openLocation({
					latitude: data.lat,
					longitude: data.lng,
					name: data.address,
					// address: data.citynames,
					success: function() {
						console.log('success');
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.order-views {
		position: relative;
		top: -100rpx;
		margin-bottom: -100rpx;

		.copy {
			// background-color: ;
		}
	}

	// 订单
	.order {

		// width: 690rpx;
		// background-color: #ffffff;
		// margin: 30rpx auto;
		// border-radius: 30rpx;
		// box-sizing: border-box;
		// padding: 30rpx;
		// font-size: 28rpx;
		// box-shadow: $u-color-border-shadow;
		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;

			.left {
				margin-right: 20rpx;

				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
				color: $u-color-orange;
			}
		}

		.time {
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				padding: 0 10rpx;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	// 按钮栏
	.navigation {
		display: flex;
		margin-top: 30rpx;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		position: sticky;
		bottom: 0;
		z-index: 99;

		.right {
			display: flex;
			font-size: 28rpx;
			align-items: center;
			flex: 1;
			justify-content: space-around;
			padding: 20rpx;

			.navbtn {
				width: 100%;
				margin: 0 10rpx;
			}

			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
				margin: 16rpx 0;
			}

			.cancel {
				margin-right: 30rpx;
				color: $u-tips-color;
			}

			.buy {
				background-color: #ff7900;
			}
		}
	}

	// 
	.u-table-box {
		margin: 20rpx 0;

		.green {
			background-color: $u-color-green;
			color: #fff !important;
		}

		.red {
			background-color: $u-color-red;
			color: #fff !important;
		}
	}

	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;

		.total-price {
			font-size: 32rpx;
			color: $u-color-orange;
		}
	}

	.u-line {
		border-bottom: 1px solid $u-border-color;
		padding-top: 30rpx;
		margin-bottom: 20rpx;
	}

	.u-order-title {
		color: #333333;
		font-weight: bold;
		font-size: 32rpx;
	}

	.u-order-desc {
		color: rgb(150, 150, 150);
		font-size: 28rpx;
		margin-bottom: 6rpx;
	}

	.u-order-time {
		color: rgb(200, 200, 200);
		font-size: 26rpx;
	}
</style>